﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
public partial class restaurant_admin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!IsLogined())
                Response.Redirect("~/restaurant/login.aspx");

            InitData();
            InitForm();
        }
    }

    protected bool IsLogined()
    {
        return (Session["owner_id"] != null);
    }

    protected void InitData()
    {
        if (Session["owner_id"] == null)
            return;

        String queryString = Session["owner_id"].ToString();
        Owner owner = new Owner();
        Int16.TryParse(queryString, out owner.Id);

        if (!BLL.Command.GetOwner(ref owner))
        {
            Session["owner_id"] = null;
            return;
        }

        username_textbox.Text = owner.Name;
        mobile_textbox.Text = owner.Phone;
        idcard_textbox.Text = owner.IdCard;

        ///
        // restaurants
        ///
        restaurant_list_datagrid.DataSource = BLL.Command.GetRestaurantsByOwner(owner.Id);
        restaurant_list_datagrid.DataBind();

        ///
        // Register the confirm msg for del_btn
        ///
        foreach (DataGridItem row in restaurant_list_datagrid.Items)
        {
            Button delBtn = (Button)row.FindControl("del_btn");
            delBtn.Attributes.Add("onclick", "return confirm('您确定要删除此餐厅吗?');");
        }
    }
    protected void InitForm()
    {
        username_textbox.Enabled = false;

        Freeze();
    }
    
    void Freeze()
    {
        pwd_lable.Visible = false;
        repwd_lable.Visible = false;
        pwd_textbox.Visible = false;
        repwd_textbox.Visible = false;
        pwd_required_validator.Visible = false;
        repwd_required_validator.Visible = false;

        mobile_textbox.ReadOnly = true;
        idcard_textbox.ReadOnly = true;

        submit_button.Enabled = false;
        edit_button.Enabled = true;
    }
    void Edit()
    {
        pwd_lable.Visible = true;
        repwd_lable.Visible = true;

        pwd_textbox.Visible = true;
        repwd_textbox.Visible = true;
        pwd_required_validator.Visible = true;
        repwd_required_validator.Visible = true;

        mobile_textbox.ReadOnly = false;
        idcard_textbox.ReadOnly = false;

        submit_button.Enabled = true;
        edit_button.Enabled = false;

        notice_lable.Text = "密码留空则不变.";
    }
    protected void UpdateNotice(String notice, bool isWarning)
    {
        notice_lable.Text = notice;

        if (isWarning)
            notice_lable.ForeColor = System.Drawing.Color.Red;
    }
    protected void OnEditBtnClick(object sender, EventArgs e)
    {
        Edit();
    }
    protected void OnSubmitBtnClick(object sender, EventArgs e)
    {
        String username = username_textbox.Text;
        String pwd = pwd_textbox.Text;
        String phone = mobile_textbox.Text;
        String idcard = idcard_textbox.Text;

        if (BLL.Command.updateOwner(username, pwd, phone, idcard))
        {
            UpdateNotice("信息修改成功!", false);
            Freeze();
        }
        else
        {
            UpdateNotice("信息修改失败!!!", true);
        }
    }
    protected void OnNewRestaurant(object sender, EventArgs e)
    {
        Server.Transfer("~/Restaurant/Restaurant.aspx");
    }
    protected void OnClickResButton(object sender, DataGridCommandEventArgs e)
    {
        Int16 restaurantId = Convert.ToInt16(restaurant_list_datagrid.DataKeys[e.Item.ItemIndex].ToString());
        //Debug.Assert(restaurantId>0);

        if (e.CommandName == "del_btn")
        {
            // delete the res
            BLL.Command.DelRestaurant(restaurantId);
            InitData();
            if (Session["restaurant_id"] != null
                && Convert.ToInt16(Session["restaurant_id"].ToString()) == restaurantId)
            Session["restaurant_id"] = null;
        }
        else if (e.CommandName == "enter_btn")
            Response.Redirect("~/Restaurant/Restaurant.aspx?restaurant_id=" + restaurantId);
    }
}
